package com.shanzmoo.admin.web.${package.ModuleName!};

import com.shanzmoo.admin.domain.dto.${entity? replace('Entity','SaveDTO')};
import com.shanzmoo.admin.domain.dto.${entity? replace('Entity','UpdateDTO')};
import ${package.Entity}.query.${entity? replace('Entity','Query')};
import ${package.Entity}.vo.${entity? replace('Entity','VO')};
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import com.shanzmoo.admin.manager.store.${entity? replace('Entity','Manager')};
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;

/**
 * ${table.comment!} 控制层
 *
 * @author ${author} ${date}
 */
@Api(tags = "${table.comment!}")
@RestController
@RequestMapping("/${package.ModuleName}/${controllerMappingHyphen? replace(package.ModuleName + '-','') ? replace('-entity','')}")
public class ${table.controllerName} {

    @Autowired
    private ${table.serviceName} ${(entity? replace('Entity','Service'))?uncap_first};
    @Autowired
    private ${entity? replace('Entity','Manager')} ${(entity? replace('Entity','Manager'))?uncap_first};

    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public SHttpResponse<PageRespBase<${entity? replace('Entity','VO')}>> pageList(@Valid @RequestBody ${entity? replace('Entity','Query')} query) {
        PageRespBase<${entity? replace('Entity','VO')}> pageList = ${(entity? replace('Entity','Manager'))?uncap_first}.queryPage(query);
        return SHttpResponse.ok(pageList);
    }

    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public SHttpResponse<Boolean> add(@Valid @RequestBody ${entity? replace('Entity','SaveDTO')} saveDto) {
        ${(entity? replace('Entity','Service'))?uncap_first}.save(saveDto.convertEntity());
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "删除")
    @GetMapping("/delete")
    public SHttpResponse<Boolean> deleteById(@RequestParam(value = "id") Integer id) {
        ${entity} entity = new ${entity}();
        entity.setId(id);
        entity.setDeleted(true);
        ${(entity? replace('Entity','Service'))?uncap_first}.updateById(entity);
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public SHttpResponse<Boolean> update(@Valid @RequestBody ${entity? replace('Entity','UpdateDTO')} updateDto) {
        ${(entity? replace('Entity','Service'))?uncap_first}.updateById(updateDto.convertEntity());
        return SHttpResponse.ok(Boolean.TRUE);
    }
 }
